Skip to content

[TwigComponent] Support component tag names from expressions and component objects#3395

Open
andreybotanic wants to merge 3 commits intosymfony:2.xfrom
andreybotanic:feature/dynamic-name-in-component-tag
Open

[TwigComponent] Support component tag names from expressions and component objects#3395
andreybotanic wants to merge 3 commits intosymfony:2.xfrom
andreybotanic:feature/dynamic-name-in-component-tag

Conversation

@andreybotanic
Copy link
Copy Markdown

@andreybotanic andreybotanic commented Mar 20, 2026

Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues
License MIT

Description

Currently, there are three ways to render a component:

  1. HTML-like syntax
  2. {% component %} tag
  3. component() function

Each approach has its own limitations:

  • The component() function supports dynamic component names, but does not allow blocks.
  • The {% component %} tag supports blocks, but does not allow dynamic component names.
  • The HTML-like syntax behaves similarly to the {% component %} tag.

Proposal

Add support for dynamic component names in the {% component %} tag.

This would enable a unified syntax that supports both:

  • dynamic component resolution
  • block usage

The HTML-like syntax is intentionally left unchanged, as introducing dynamic names there would result in awkward and hard-to-read syntax.

Example Usage

{% set componentName = 'MyComponent' %}
{% component componentName %}
{% endcomponent %}

Extended Idea

Allow passing a component instance directly, instead of its name:

{% component myComponent %}
{% endcomponent %}

Where myComponent is an object of a class marked with:

#[AsTwigComponent]

or

#[AsLiveComponent]

Real-world Use Case

This proposal was inspired by a real scenario in my project:

{% for tab in this.tabs %}
    {% component tab with {loading: 'lazy'} %}
        {% block loadingContent %}
            <div class="d-flex align-items-center w-75 mx-auto">
                <strong role="status">Loading...</strong>
                <div class="spinner-border ms-auto" aria-hidden="true"></div>
            </div>
        {% endblock %}
    {% endcomponent %}
{% endfor %}

In this case, tab is dynamic, and there is a need to:

  • pass props (loading)
  • define custom blocks (loadingContent)

Currently, this is not achievable with existing syntax without compromises.

Benefits

  • Eliminates inconsistency between rendering methods
  • Enables more flexible and expressive templates
  • Improves DX when working with dynamic UI structures (e.g. tabs, registries, dynamic components)

Notes

  • No changes proposed for HTML-like syntax
  • Backward compatibility is preserved

@carsonbot carsonbot added Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed labels Mar 20, 2026
@carsonbot carsonbot changed the title Support component tag names from expressions and component objects Support component tag names from expressions and component objects Mar 20, 2026
@Kocal Kocal requested a review from smnandre March 21, 2026 22:22
@carsonbot carsonbot changed the title Support component tag names from expressions and component objects [TwigComponent] Support component tag names from expressions and component objects Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed TwigComponent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants